package com.maila.xiaoaoschool.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.api.LoginApi;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.maila.xiaoaoschool.util.AlertUtil;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新增更新收货地址
 * Created by mark on 2017/10/31.
 */

public class AddaddressActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.save_information_tv)
    TextView saveInformationTv;
    @BindView(R.id.ll_title)
    AutoLinearLayout llTitle;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.bnt_location)
    ImageView bntLocation;
    @BindView(R.id.provincialCity_tv)
    TextView provincialCityTv;
    @BindView(R.id.edit_provincialCity)
    AutoLinearLayout editProvincialCity;
    @BindView(R.id.edit_detailed_address)
    EditText editDetailedAddress;
    @BindView(R.id.default_switch)
    RadioButton defaultSwitch;
    @BindView(R.id.is_default)
    AutoRelativeLayout rlisDefault;
    String adcode;
    boolean phoneOK;
    boolean nameOk;
    boolean addressOk;
    int type = 1;
    String addressID;
    @BindView(R.id.btn_del_address)
    TextView btnDelAddress;
    private String lat;
    private String lng;
    boolean isDefault = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        ButterKnife.bind(this);
        if (type == 1) {
            btnDelAddress.setVisibility(View.VISIBLE);
        } else {
            btnDelAddress.setVisibility(View.GONE);
        }
        initlistner();
    }

    /**
     *
     */
    private void initlistner() {
        editDetailedAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 5) {
                    addressOk = true;
                } else {
                    addressOk = false;
                }

            }
        });
        editName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    nameOk = true;
                } else {
                    nameOk = false;
                }

            }
        });
        editPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    phoneOK = true;
                } else {
                    phoneOK = false;
                }
            }
        });

        defaultSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isDefault = isChecked;
            }
        });
    }

    @OnClick({R.id.white_return_iv, R.id.save_information_tv, R.id.bnt_location, R.id.provincialCity_tv, R.id.btn_del_address, R.id.default_switch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.default_switch:


                break;
            case R.id.save_information_tv:
                if (phoneOK && nameOk && addressOk) {
                    new LoginApi(this, new VolleyInterface() {
                        @Override
                        public void onSuccess(String result) {
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                if (jsonObject.optInt("result") == 1) {

                                    if(isDefault){

                                        new LoginApi(AddaddressActivity.this, new VolleyInterface() {
                                            @Override
                                            public void onSuccess(String result) {
                                                try {
                                                    JSONObject   jsonObject=new JSONObject(result);
                                                    if(jsonObject.optInt("result") == 1){
                                                        if(type==1){
                                                            Toast.makeText(AddaddressActivity.this, "修改完成", Toast.LENGTH_SHORT).show();
                                                        }else {
                                                            Toast.makeText(AddaddressActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                                        }
                                                        finish();
                                                    }else {

                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }

                                            @Override
                                            public void onError(VolleyError error) {

                                            }
                                        }).set_default_address(jsonObject.optString("data"));
                                    }else {
                                        if(type==1){
                                            Toast.makeText(AddaddressActivity.this, "修改完成", Toast.LENGTH_SHORT).show();
                                        }else {
                                            Toast.makeText(AddaddressActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                        }
                                        finish();
                                    }

                                } else {
                                    Toast.makeText(AddaddressActivity.this, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(VolleyError error) {

                        }
                    }).add_address(editPhone.getText().toString(), editName.getText().toString(), adcode, editDetailedAddress.getText().toString(), type, addressID, lat, lng);

                } else {
                    Toast.makeText(this, "请正确填写地址信息", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bnt_location:
                break;
            case R.id.provincialCity_tv:
                break;
            case R.id.btn_del_address:
                AlertUtil.ShowDialog(this, "是否删除该收货地址?", "删除后无法恢复", new AlertUtil.Alertlinstner() {
                    @Override
                    public void clickConfirm() {
                        new LoginApi(AddaddressActivity.this, new VolleyInterface() {
                            @Override
                            public void onSuccess(String result) {
                                try {
                                    JSONObject jsonObject = new JSONObject(result);
                                    if (jsonObject.optInt("result") == 1) {
                                        Toast.makeText(AddaddressActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                                        finish();
                                    } else {
                                        Toast.makeText(AddaddressActivity.this, "" + jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }

                            @Override
                            public void onError(VolleyError error) {

                            }
                        }).del_address(addressID);
                    }

                    @Override
                    public void clickSingle(int position) {

                    }
                });
                break;
        }
    }
}
